<template>
  <div class="address">
    <project-nav-bar></project-nav-bar>
    <add-tab-bar @addClick="addClick">
      <span slot="title">+新建地址</span>
    </add-tab-bar>
    <div class="address-main">
      <div class="address-item" v-for="item in addressList" :key="item.id">
        <div class="address-item-left">
          <div class="name">{{ item.name }}</div>
          <div v-if="item.is_default == 1" class="defaults">默认</div>
        </div>
        <div class="address-item-center" @click="addressClick(item)">
          <div class="phone">{{ item.mobile }}</div>
          <div class="address">
            {{ item.address }} {{ item.address_detail }}
          </div>
        </div>
        <div class="address-item-right">
          <div class="edit" @click="editClick(item)">
            <img src="@/assets/icon/edit.png" alt="" />
          </div>
          <div class="edit del" @click="delClick(item)">
            <van-icon name="delete" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ProjectNavBar from "./children/ProjectNavBar";
import AddTabBar from "./children/AddTabBar";
import { getListAction, getDeleteAction } from "@/network/address";
import { getDetailAction } from "@/network/payload";
export default {
  components: {
    ProjectNavBar,
    AddTabBar,
  },
  created() {
    this.getListActions();
  },
  data() {
    return {
      addressList: [],
    };
  },
  methods: {
    getListActions() {
      getListAction({
        openId: localStorage.getItem("openId"),
      }).then((res) => {
        console.log(res);
        this.addressList = res.data;
      });
    },
    addressClick(item) {
      // item.is_default = 1;
      // console.log(item);
      let addressId = item.id;
      console.log(item);
      // this.$router.go(-1)
      this.$router.push({
        path: '/payload',
        query: {
          id: item.id
        }
      })

      // this.addClicks(item);
    },
    // 跳到添加页面
    addClick() {
      console.log(1);
      this.$router.push({
        path: "addaddress",
      });
    },
    editClick(item) {
      console.log(item.id);
      this.$router.push("/addaddress/" + item.id); // params传值
    },
    delClick(item) {
      console.log(item.id);
      getDeleteAction({
        id: item.id,
      }).then((res) => {
        console.log(res);
        this.getListActions();
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.address-main {
  // margin-top: 49px;
  margin: 49px 10px 0;
  .address-item {
    display: flex;
    height: 66px;
    padding: 17px 0;
    border-bottom: 2px solid #ececec;
    .address-item-left {
      width: 20%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
      .defaults {
        padding: 2px 4px;
        border: 1px solid #bb3c41;
        color: #bb3c41;
      }
    }
    .address-item-center {
      width: 60%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
    }
    .address-item-right {
      width: 20%;
      display: flex;
      // flex-direction: column;
      justify-content: center;
      align-items: center; // 居中
      .edit {
        width: 28px;
        margin: 5px;
        img {
          width: 100%;
        }
      }
      .del {
        color: #bb3c41;
        font-size: 26px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>